﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using LastoRevival.Service.Implementations.Settings;
using System.Configuration;
using System.Net;

namespace LastoRevival.Service.Implementations.Core
{
	public class Emailer
	{

		private SmtpClient client;

		private Emailer()
		{

		}

		public static Emailer Create()
		{			
			var client = new SmtpClient();

			client.Host = ServiceSettings.GetInstance().SmtpServer;
			client.Port = ServiceSettings.GetInstance().SmtpPort;
			client.EnableSsl = ServiceSettings.GetInstance().SmtpSsl;
			//client.Timeout = 

			// todo optional credentials
			client.UseDefaultCredentials = false;
			client.Credentials = new NetworkCredential(ServiceSettings.GetInstance().SmtpUser, ServiceSettings.GetInstance().SmtpPassword);

			return new Emailer { client = client };
		}

		public void SendEmail(string to, string from, string subject, string body)
		{
			var message = new MailMessage();
			message.To.Add(to);
			message.From = new MailAddress(from);
			message.Subject = subject;
			message.IsBodyHtml = true;
			message.Body = body;

			client.SendAsync(message, null);
		}


	}
}
